//
//  HCMineCollectViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/5/31.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCMineCollectViewController.h"

#import "HCMineCollectTableViewCell.h"

#import "HCMineCollectModel.h"

@interface HCMineCollectViewController () <UITableViewDelegate,UITableViewDataSource> {
    UITableView *tableView_;
    HCMineCollectModel *model_;
}

@end

@implementation HCMineCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"我的收藏";
    
    tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    tableView_.backgroundColor = [UIColor whiteColor];
    tableView_.showsVerticalScrollIndicator = NO;
    tableView_.delegate = self;
    tableView_.dataSource = self;
    tableView_.rowHeight = YBKAdaptiveH(90);
    tableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.centerView addSubview:tableView_];
    self.dataRequest.delegate = self;
    //注册cell
    [tableView_ registerNib:[UINib nibWithNibName:@"HCMineCollectTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"collect"];
    
    tableView_.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataRequest post_HCMineCollor:HCGETUSER(@"userid")];
    }];
    
    // Do any additional setup after loading the view.
}

#pragma mark 代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (model_) {
        return model_.collect_list.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCMineCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collect" forIndexPath:indexPath];
    if (model_) {
        cell.model = (HCMineCollectModel *)model_.collect_list[indexPath.row];
        cell.cancleBtn.tag = 2;
        [cell.cancleBtn addTarget:self action:@selector(editorButton:event:) forControlEvents:UIControlEventTouchUpInside];
        
        cell.duihuanBtn.tag = 1;
        [cell.duihuanBtn addTarget:self action:@selector(editorButton:event:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [tableView_.mj_header beginRefreshing];
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {

    if (aType == HC_MINECOLLECT_TYPE) {
        NSLog(@"我的收藏成功:%@",dictionary);
        model_ = [[HCMineCollectModel alloc] initWithDictionary:dictionary error:nil];
        [tableView_ reloadData];
        [tableView_.mj_header endRefreshing];
    }else{
        NSLog(@"取消收藏成功:%@",dictionary);
        [tableView_.mj_header beginRefreshing];
    }
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"我的收藏失败");
    [tableView_.mj_header endRefreshing];
}

#pragma mark 获取选中的indexPath
-(void)editorButton:(UIButton *)sender event:(id)event {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:tableView_];
    NSIndexPath *indexPath = [tableView_ indexPathForRowAtPoint:currentTouchPosition];
    
    //TODO 通过indexPath来确定需要传递什么参数
    HCMineCollectData *data = model_.collect_list[indexPath.row];

    if (sender.tag == 1) {
        //去兑换
        NSLog(@"去兑换");
        
        
        [self pushChildrenViewController:@"HCGGoodsDetailViewController" parameterObject:data dataObject:data.url];
    }else{
        //取消收藏
        NSLog(@"取消收藏");
        [self.dataRequest post_HCMineCollectCancle:data.hc_id user:HCGETUSER(@"userid")];
    }

    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
